﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UFIDA.U8.YYNBJY.Operate
{
    public class TBCheckVouch
    {
        public static string CheckVouch(string filename, string dbname, string cvouchid, bool ilog, bool iilog)
        {
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename + "]方法START======================", true, ilog, iilog, System.Drawing.Color.Black);
            try
            {
                string condition = " and 1=1 ";
                if (!string.IsNullOrEmpty(cvouchid))
                {
                    condition += " and cCVCode='" + cvouchid + "' ";
                }
                string sql = @"SELECT ID,cCVCode,dCVDate,cWhCode FROM " + dbname + @"..CheckVouch 
                                where 1 = 1 and cWhCode in ('05','21') And ISNULL(dnverifytime,'')='' and ID not in (select MODID from EF_LF_MESU8..EF_CheckVouch where DBNAME = '" + dbname + @"')" + condition + " order by cCVCode";
                DataTable dt = DBHelp.SQLHelper.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    JSONDATA.CHECKVOUCH jsm = new JSONDATA.CHECKVOUCH();
                    jsm.GUID = Guid.NewGuid().ToString();
                    jsm.TRXID = "InventoryCount";
                    JSONDATA.DATAItemC jdp = new JSONDATA.DATAItemC();
                    List<JSONDATA.DATAItemC> lid = new List<JSONDATA.DATAItemC>();
                    List<JSONDATA.ITEMDATAItemC> lit = new List<JSONDATA.ITEMDATAItemC>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        jdp = new JSONDATA.DATAItemC();
                        string cCVCode = dt.Rows[i]["cCVCode"].ToString();
                        string ID = dt.Rows[i]["ID"].ToString();
                        if (!string.IsNullOrEmpty(ID))
                        {
                            lit = new List<JSONDATA.ITEMDATAItemC>();
                            
                            sql = @"SELECT cInvCode FROM " + dbname + "..CheckVouchs where ID='" + ID + "' group by cInvCode";
                            DataTable ds = DBHelp.SQLHelper.GetDataTable(sql);
                            for (int j = 0; j < ds.Rows.Count; j++)
                            {
                                JSONDATA.ITEMDATAItemC jit = new JSONDATA.ITEMDATAItemC();
                                //jit.SEQ = ds.Rows[j]["autoID"].ToString();
                                jit.ITEM_ID = ds.Rows[j]["cInvCode"].ToString();
                                lit.Add(jit);
                            }
                            jdp.SHEET_ID = cCVCode;
                            jdp.STRAGE_ID = dt.Rows[i]["cWhCode"].ToString();
                            jdp.COUNT_DATE_TIME = dt.Rows[i]["dCVDate"].ToString();
                            jdp.ITEMDATA = lit;
                        }
                        else
                        {
                            PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]单号[" + cCVCode + "]没有查到明细行", true, ilog, iilog, System.Drawing.Color.Red);
                            continue;
                        }
                        lid.Add(jdp);
                        jsm.DATA = lid;
                    }
                    string json = JsonConvert.SerializeObject(jsm);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]调用[" + filename + "]参数:" + json, false, ilog, iilog, System.Drawing.Color.Red);
                    string result = PUBMethod.PMethod.CreatePostHttpResponses(Model.PUBCLASS.MESURL, json, null);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]返回参数:" + result, false, ilog, iilog, System.Drawing.Color.Red);
                    if (!string.IsNullOrEmpty(result))
                    {
                        if (result.Substring(0, 5) != "false")
                        {
                            JSONDATA.RESJSON jre = JsonConvert.DeserializeObject<JSONDATA.RESJSON>(result);
                            string GUID = jre.GUID;
                            string RTNCODE = jre.RTNCODE;
                            string RTNMSG = jre.RTNMSG;
                            if (RTNCODE == "0")
                            {
                                List<string> lis = new List<string>();
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]同步成功:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Green);
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    string cCVCode = dt.Rows[i]["cCVCode"].ToString();
                                    string ID = dt.Rows[i]["ID"].ToString();
                                    lis.Add("insert into EF_LF_MESU8..EF_CheckVouch(DBNAME,MOCODE,MODID,CREATETIME,DEFINE1)values('" + dbname + "','" + cCVCode + "','" + ID + "',GETDATE(),'" + GUID + "') ");
                                }
                                DBHelp.SQLHelper.ExecuteSqlTran(lis);
                            }
                            else
                            {
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]同步失败:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Red);
                            }
                        }
                        else
                        {
                            PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]接口返回异常:" + result, true, ilog, iilog, System.Drawing.Color.Red);
                        }
                    }
                    else
                    {
                        PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]接口返回空", true, ilog, iilog, System.Drawing.Color.Red);
                    }

                }
                else
                {
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]没有查到需要同步的[" + filename + "]", true, ilog, iilog, System.Drawing.Color.Red);
                }

            }
            catch (Exception ex)
            {
                PUBMethod.PMethod.printmsgs(filename, "[" + filename + "]同步异常:" + ex.Message, true, ilog, iilog, System.Drawing.Color.Red);
            }
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename + "]方法E N D======================", true, ilog, iilog, System.Drawing.Color.Black);
            return "";
        }


    }
}
